﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using prjBULHRM;
namespace prjQuanLyNhanSu
{
    public partial class frmQuocTich : Form
    {
        public frmQuocTich()
        {
            InitializeComponent();
        }
        clsBULTT_QuocTich cls;
        public void LoadDuLieu()
        {
            cls = new clsBULTT_QuocTich();
            dgrQuocTich.DataSource = cls.SelectAll();
            btnSua.Text = "&Sửa";
            btnThem.Text = "&Thêm";
            txtMaQT.Enabled = false;
            txtTenQT.Enabled = false;
        }
                
        private void frmQuocTich_Load(object sender, EventArgs e)
        {
            LoadDuLieu();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (btnThem.Text == "&Thêm")
            {
                btnThem.Text = "&Lưu Thêm";
                txtMaQT.Enabled = true;
                txtTenQT.Enabled = true;
                txtMaQT.Text = "";
                txtTenQT.Text = "";
                txtMaQT.Focus();
                btnSua.Enabled = false;
                btnXoa.Enabled = false;
            }
            else
            {
                if (txtMaQT.Text == "" || txtTenQT.Text == "")
                {
                    MessageBox.Show("Thiếu thông tin dữ liệu.", "Thiếu dữ liệu", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {                   
                    cls = new clsBULTT_QuocTich();
                    cls.QuocTich_Id = txtMaQT.Text;
                    cls.Name = txtTenQT.Text;
                    if (cls.TrungMa() == true)
                    {
                        MessageBox.Show("Trùng mã", "Lỗi Trùng", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {                        
                        cls.Insert();
                        LoadDuLieu();
                        MessageBox.Show("Thêm thành công !");
                        btnSua.Enabled = true;
                        btnXoa.Enabled = true;
                    }
                }
            }
        }


        private void btnSua_Click(object sender, EventArgs e)
        {
            if (btnSua.Text == "&Sửa")
            {
                if (txtMaQT.Text == "")
                {
                    MessageBox.Show("Bạn chưa chọn dữ liệu để sửa.", "Lỗi Dữ Liệu", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    btnSua.Text = "&Lưu Sửa";
                    txtMaQT.Enabled = false;
                    txtTenQT.Enabled = true;
                    txtTenQT.Focus();
                    btnThem.Enabled = false;
                    btnXoa.Enabled = false;
                    dgrQuocTich.Enabled = false;
                }
            }
            else
            {
                try
                {                    
                    btnSua.Text = "&Sửa";
                    cls = new clsBULTT_QuocTich();
                    cls.QuocTich_Id = txtMaQT.Text;
                    cls.Name = txtTenQT.Text;
                    if (txtMaQT.Text==""||txtTenQT.Text == "")
                    {
                        MessageBox.Show("Không để trống.");
                    }
                    else
                    {
                        cls.Update();
                        LoadDuLieu();
                        MessageBox.Show("Sửa thành công!");
                        btnThem.Enabled = true;
                        btnXoa.Enabled = true;
                        dgrQuocTich.Enabled = true;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Không sửa được.");
                }
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (txtMaQT.Text == "")
            {
                MessageBox.Show("Bạn chưa chọn dữ liệu để xóa.","Lỗi Dữ Liêu",MessageBoxButtons.OK,MessageBoxIcon.Warning);
            }
            else
            {
                DialogResult hoi = MessageBox.Show("Bạn có chắc muốn xóa dữ liệu này không?", "Xóa dữ liệu", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (hoi == DialogResult.Yes)
                {
                    try
                    {
                        cls = new clsBULTT_QuocTich();
                        cls.QuocTich_Id = txtMaQT.Text;
                        cls.Delete();
                        txtMaQT.Text = "";
                        txtTenQT.Text = "";
                        LoadDuLieu();
                        MessageBox.Show("Xóa thành công!");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Không xóa dữ liệu này được.");
                    }
                }
            }
        }

        private void dgrQuocTich_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            txtMaQT.Text = dgrQuocTich.CurrentRow.Cells[0].Value.ToString();
            txtTenQT.Text = dgrQuocTich.CurrentRow.Cells[1].Value.ToString();
        }

        private void dgrQuocTich_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            txtMaQT.Enabled = false;
            txtTenQT.Enabled = false;

            btnSua.Enabled = true;
            btnThem.Enabled = true;
            btnXoa.Enabled = true;

            btnSua.Text = "&Sửa";
            btnThem.Text = "&Thêm";
        }
    }
}
